package com.ogilvy.audi.webapp.action;

import java.util.List;

import com.ogilvy.audi.model.CircleTopic;
import com.ogilvy.audi.service.CircleTopicManager;
import com.ogilvy.audi.util.ServiceDispatcher;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.Preparable;

public class CircleTopicAction extends BaseAction implements Preparable {
    private CircleTopicManager circleTopicManager;
    private List circleTopics;
    private CircleTopic circleTopic;
    private Long topicId;
    private String query;
    private List<CircleTopic> topListSimple;
    private String circleId;
    public List<?> topList;
    private CircleTopic topic;
    public ServiceDispatcher serviceDispatcher;
    public void setCircleTopicManager(CircleTopicManager circleTopicManager) {
        this.circleTopicManager = circleTopicManager;
    }

    public List getCircleTopics() {
    	
        return circleTopics;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String circleTopicId = getRequest().getParameter("circleTopic.topicId");
            if (circleTopicId != null && !circleTopicId.equals("")) {
                circleTopic = circleTopicManager.get(new Long(circleTopicId));
            }
        }
    }

    public void setQ(String q) {
        this.query = q;
    }

    public String list() {
       // circleTopics = circleTopicManager.search(query, CircleTopic.class);
    	circleId = defValfiledId(circleId);
    	topListSimple = serviceDispatcher.getCircleTopicManager().getRecordsByHql("from CircleTopic where circleId = " +circleId,null);
        return "list";
    }
    public String detail() {
		System.out.println("__________________________________________________________------enter topicAction.detail()");
		return "detail";
	}
    public String show() {
		System.out.println("__________________________________________________________------enter topicAction.show()");
		topic = new CircleTopic();
		//topic.setTacticTitle("jjj");
		//topic.setTopicDesc("ooopppddesccc");
		return "send";
	}
    public String add() {
		System.out.println("_________________________________________topic  "+topic);
		
		CircleTopic circleTopic2 = serviceDispatcher.getCircleTopicManager().save(circleTopic);
		
		ActionContext.getContext().put("msg", "发表成功！");
		//json
		return null;
	}
    public void setTopicId(Long topicId) {
        this.topicId = topicId;
        
    }

    public CircleTopic getCircleTopic() {
        return circleTopic;
        
    }

    public void setCircleTopic(CircleTopic circleTopic){
        this.circleTopic = circleTopic;
    }

    public String delete(){
        circleTopicManager.remove(circleTopic.getTopicId());
        saveMessage(getText("circleTopic.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (topicId != null) {
            circleTopic = circleTopicManager.get(topicId);
        } else {
            circleTopic = new CircleTopic();
        }

        return SUCCESS;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (circleTopic.getTopicId() == null);

        circleTopicManager.save(circleTopic);

        String key = (isNew) ? "circleTopic.added" : "circleTopic.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }
    public String defValfiledId(String sid){
    	if(sid==null){
    		return "1";
    	}else if(sid.equals("")){
    		return "1";
    	}else{
    		return sid;
    	}
    	
    }
    
    
    
}